package com.atguigu.juc.date930;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author: shenhao
 * @CreatTime: 2024/9/30 9:46
 * @Version: 1.0
 * callable接口同眼
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
            System.out.println("....come in call");
            int sum = 0;
            for (int i = 0; i < 100; i++) {
                sum += i;
            }
            TimeUnit.SECONDS.sleep(5);
            return sum;
        });
        new Thread(futureTask).start();// 同一个任务 只会调用一次,结果可以复用只计算一次
        FutureTask<String> stringFutureTask = new FutureTask<>(new myCallable());
        new Thread(stringFutureTask).start();
        System.out.println(stringFutureTask.get());
        System.out.println(futureTask.get());// get方法最后调用
    }
}

class myCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        String s1 = "hello World!!!";
        System.out.println(s1);
        return s1+"good Morning";
    }
}
